/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */
/**
 * Created by zzz on 2019/10/24.
 * IP、IP范围或子网
 */

import Validator from '../validation/validator';
import SfVueComponent from '@sxf/sf-vue-component';
import IPRange from './ipv4_range';

let ipv4Validator = SfVueComponent.vtypes.get('ipv4').validator;
let maskv4Validator = SfVueComponent.vtypes.get('maskv4').validator;
let iprangeValidator = new IPRange();

export default class IpRangeOrMaskValidator extends Validator {

    verify (v) {
        let ret = true;
        if (/\//.test(v)) {
            let [ipVal, maskVal] = v.split('/');
            ret = ipv4Validator.verify(ipVal);
            if (ret !== true) {
                return ret;
            }
            ret = maskv4Validator.verify(maskVal);
            return ret;
        } else if (/-/.test(v)) {
            return iprangeValidator.verify(v);
        } else if (/\./.test(v)) {
            return ipv4Validator.verify(v);
        }

        return _('只能输入IP、IP范围或子网');
    }
}
